package com.chat.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.admin.domain.DTO.UserAppealDTO;
import com.chat.admin.domain.DTO.UserFeedbackDTO;
import com.chat.admin.domain.VO.RoleVO;
import com.chat.admin.domain.VO.UserFeedbackVO;
import com.chat.admin.mapper.UserFeedbackMapper;
import com.chat.admin.service.IUserFeedbackService;
import com.chat.common.entity.Role;
import com.chat.common.entity.UserFeedback;
import com.chat.common.form.ListForm;
import com.chat.common.vo.R;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weikjie
 * @since 2024-05-13
 */
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements IUserFeedbackService {

    @Override
    public ListForm<UserFeedbackVO> getUserFeedbacks(UserFeedbackDTO userFeedbackDTO) {
        QueryWrapper<UserFeedback> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(userFeedbackDTO.getAccount())){
            queryWrapper.eq("account",userFeedbackDTO.getAccount());
        }
        if(userFeedbackDTO.getType() != null){
            queryWrapper.like("type",userFeedbackDTO.getType());
        }
        if(userFeedbackDTO.getProcessing_result() != null){
            queryWrapper.eq("processing_result", userFeedbackDTO.getProcessing_result());
        }
        Page<UserFeedback> userFeedbackPage = new Page<>(userFeedbackDTO.getPage(), userFeedbackDTO.getSize());
        IPage<UserFeedback> userFeedbackIPage = page(userFeedbackPage, queryWrapper);
        List<UserFeedback> userFeedbackList = userFeedbackIPage.getRecords();
        List<UserFeedbackVO> userFeedbackVOList = userFeedbackList.stream().map(userFeedback -> {
            UserFeedbackVO userFeedbackVO = new UserFeedbackVO();
            BeanUtil.copyProperties(userFeedback, userFeedbackVO);
            return userFeedbackVO;
        }).toList();
        return new ListForm<>(userFeedbackDTO.getPage(),userFeedbackDTO.getSize(),
                userFeedbackIPage.getPages(), //总页数
                userFeedbackIPage.getTotal(), //总记录数total
                userFeedbackVOList);
    }

    @Override
    public Integer HandleUserFeedback(String ID) {
        UserFeedback userFeedback = baseMapper.selectById(ID);
        if(userFeedback == null){
            return -1;
        }
        userFeedback.setProcessingTime(LocalDateTime.now());
        userFeedback.setProcessingResult(true);
        return baseMapper.updateById(userFeedback);
    }
}